/**
 * 用户密码登录错误次数达到五次后，锁定用户
 */
package com.catsic.security.authority;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

import com.catsic.base.utils.SpringContextUtil;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.core.user.service.UserService;

public class UserAuthenticationProvider extends DaoAuthenticationProvider{
	
	@Autowired
	private UserService userService;
	
	@Override
    public Authentication authenticate(Authentication authentication)
            throws AuthenticationException {
        try {
            Authentication auth = super.authenticate(authentication);
            userService.resetFailureCount(authentication.getName());
            return auth;
        } catch (BadCredentialsException e) {
        	Integer params[] = userService.updateFailureCount(authentication.getName());
        	if(params[0] >= 3){
        		throw new BadCredentialsException(SpringContextUtil.getMessage(CatsicExceptionCode.用户管理_密码错误次数,params));
        	}
            throw e;
        }
    }
}
